/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.network.topo.polyline;

import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Iterator;

import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

import twaver.Element;
import twaver.PolyLine;
import twaver.Segment;
import twaver.TDataBox;
import twaver.TWaverConst;
import twaver.TerminalPoint;
import twaver.Node;
import twaver.network.TNetwork;
import twaver.network.ui.PolyLineUI;
import twaver.TWaverUtil;

public class Network1 extends TNetwork{

	private PolyLine polyline = new PolyLine();


	public Network1(){
		this.setToolbarByName(TWaverConst.DEFAULT_TOOLBAR);
		this.createPolyLine();
		this.interaction();
	}

	private void interaction(){
		this.getCanvas().addMouseListener(new MouseAdapter(){
			public void mouseClicked(MouseEvent e) {
				if(!SwingUtilities.isLeftMouseButton(e) || e.getClickCount() != 2){
					return;
				}
				TDataBox box = getDataBox();
				Iterator it = box.iterator();
				while(it.hasNext()){
					Element element = (Element)it.next();
					if(Boolean.FALSE.equals(element.getClientProperty("isAllNodeLoaded"))){
						return;
					}
					if(element instanceof PolyLine){
						PolyLineUI ui = (PolyLineUI)getRenderer().getElementUI(element);
						Segment segment = ui.getSegmentPhysicalAt(e.getPoint().x, e.getPoint().y);
						if(segment != null){
							JOptionPane.showMessageDialog(Network1.this, segment.getClientProperty("name"));
							return;
						}
					}
				}
			}
		});
	}

	private void createPolyLine(){
		polyline.setName("PolyLine1");

		TDataBox box = this.getDataBox();
		final Node fromNode = new TerminalPoint();
		fromNode.setLocation(30, 60);
		fromNode.setName("FromNode");
		box.addElement(fromNode);

		final Node toNode = new TerminalPoint();
		toNode.setLocation(600, 60);
		toNode.setName("ToNode");
		box.addElement(toNode);

        polyline.putLinkColor(Color.GREEN);
        polyline.putLinkWidth(6);
        polyline.putLinkOutlineWidth(1);

        Node lastNode = fromNode;
		for(int i=1; i<=10; i++){
			Node node = new TerminalPoint();
			node.setName("Node_" + i);
			node.setLocation(30 + 50 * i, 50 + TWaverUtil.getRandomInt(40));
			box.addElement(node);
			Segment segment = new Segment(lastNode, node);
			segment.putClientProperty("name", "Segment_" + i);
			if(i == 3){
				segment.setVisible(false);
			}
			if(i == 5){
				segment.setLineColor(Color.BLUE);
			}
			if(i == 8){
				segment.setLineColor(Color.RED);
			}

			polyline.addSegment(segment);
			lastNode = node;
		}
		Segment segment = new Segment(lastNode, toNode);
		polyline.addSegment(segment);
		segment.putClientProperty("name", "LastSegment");
		box.addElement(polyline);
	}

}